/*     */ package com.zimbra.soap;
/*     */ 
/*     */ import com.google.common.annotations.VisibleForTesting;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.ServerBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.Version;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.Reasons;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.session.AdminSession;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.session.Session.Type;
/*     */ import com.zimbra.cs.session.SessionCache;
/*     */ import com.zimbra.cs.session.SoapSession;
/*     */ import com.zimbra.cs.session.SoapSession.DelegateSession;
/*     */ import com.zimbra.cs.util.Zimbra;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Random;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class DocumentHandler
/*     */ {
/*     */   private QName responseQName;
/*  61 */   private long proxyTimeout = -1L;
/*     */   
/*     */   @VisibleForTesting
/*     */   public void setResponseQName(QName response) {
/*  65 */     this.responseQName = response;
/*     */   }
/*     */   
/*     */   protected Element getResponseElement(ZimbraSoapContext zc) {
/*  69 */     return zc.createElement(this.responseQName);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected void setProxyTimeout(long timeoutMsecs)
/*     */   {
/*  78 */     this.proxyTimeout = timeoutMsecs;
/*     */   }
/*     */   
/*  81 */   private static String LOCAL_HOST = "";
/*  82 */   private static String LOCAL_HOST_ID = "";
/*     */   
/*     */   public static String getLocalHost() {
/*  85 */     synchronized (LOCAL_HOST) {
/*  86 */       if (LOCAL_HOST.length() == 0) {
/*     */         try {
/*  88 */           Server localServer = Provisioning.getInstance().getLocalServer();
/*  89 */           LOCAL_HOST = localServer.getAttr("zimbraServiceHostname");
/*  90 */           LOCAL_HOST_ID = localServer.getId();
/*     */         } catch (Exception e) {
/*  92 */           Zimbra.halt("could not fetch local server name from LDAP for request proxying");
/*     */         }
/*     */       }
/*     */     }
/*  96 */     return LOCAL_HOST;
/*     */   }
/*     */   
/*     */   public static String getLocalHostId() {
/* 100 */     if (LOCAL_HOST_ID.length() == 0) {
/* 101 */       getLocalHost();
/*     */     }
/* 103 */     return LOCAL_HOST_ID;
/*     */   }
/*     */   
/*     */   public void preProxy(Element request, Map<String, Object> context) throws ServiceException
/*     */   {}
/*     */   
/*     */   public void postProxy(Element request, Element response, Map<String, Object> context) throws ServiceException
/*     */   {}
/*     */   
/*     */   public abstract Element handle(Element paramElement, Map<String, Object> paramMap) throws ServiceException;
/*     */   
/*     */   public static ZimbraSoapContext getZimbraSoapContext(Map<String, Object> context)
/*     */   {
/* 116 */     return (ZimbraSoapContext)context.get("zimbra.context");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static OperationContext getOperationContext(ZimbraSoapContext zsc, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/* 131 */     return getOperationContext(zsc, context == null ? null : (Session)context.get("zimbra.session"));
/*     */   }
/*     */   
/*     */   public static OperationContext getOperationContext(ZimbraSoapContext zsc, Session session) throws ServiceException {
/* 135 */     AuthToken at = zsc.getAuthToken();
/* 136 */     OperationContext octxt = new OperationContext(at);
/* 137 */     octxt.setChangeConstraint(zsc.getChangeConstraintType(), zsc.getChangeConstraintLimit());
/* 138 */     octxt.setRequestIP(zsc.getRequestIP()).setSession(session);
/* 139 */     octxt.setUserAgent(zsc.getUserAgent());
/* 140 */     return octxt;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static Account getAuthenticatedAccount(ZimbraSoapContext zsc)
/*     */     throws ServiceException
/*     */   {
/* 148 */     String id = zsc.getAuthtokenAccountId();
/* 149 */     AuthToken at = zsc.getAuthToken();
/*     */     
/* 151 */     if (("99999999-9999-9999-9999-999999999999".equals(id)) || ((at != null) && (!at.isZimbraUser()))) {
/* 152 */       return new GuestAccount(at);
/*     */     }
/*     */     
/* 155 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.id, id, zsc.getAuthToken());
/* 156 */     if (acct == null) {
/* 157 */       throw ServiceException.AUTH_REQUIRED();
/*     */     }
/* 159 */     return acct;
/*     */   }
/*     */   
/*     */   public static Account getRequestedAccount(ZimbraSoapContext zsc) throws ServiceException {
/* 163 */     String id = zsc.getRequestedAccountId();
/*     */     
/* 165 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.id, id, zsc.getAuthToken());
/* 166 */     if (acct == null) {
/* 167 */       if (zsc.isDelegatedRequest()) {
/* 168 */         throw ServiceException.DEFEND_ACCOUNT_HARVEST(id);
/*     */       }
/* 170 */       throw ServiceException.AUTH_EXPIRED();
/*     */     }
/*     */     
/*     */ 
/* 174 */     return acct;
/*     */   }
/*     */   
/*     */   public static Mailbox getRequestedMailbox(ZimbraSoapContext zsc) throws ServiceException {
/* 178 */     return getRequestedMailbox(zsc, true);
/*     */   }
/*     */   
/*     */   public static Mailbox getRequestedMailbox(ZimbraSoapContext zsc, boolean autoCreate) throws ServiceException {
/* 182 */     String id = zsc.getRequestedAccountId();
/* 183 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(id, autoCreate);
/* 184 */     if (mbox != null) {
/* 185 */       ZimbraLog.addMboxToContext(mbox.getId());
/*     */     }
/* 187 */     return mbox;
/*     */   }
/*     */   
/*     */   public boolean needsAuth(Map<String, Object> context)
/*     */   {
/* 192 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean needsAdminAuth(Map<String, Object> context)
/*     */   {
/* 198 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean handlesAccountHarvesting()
/*     */   {
/* 208 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean defendsAgainstDelegateAdminAccountHarvesting()
/*     */   {
/* 216 */     return false;
/*     */   }
/*     */   
/*     */   public Boolean canAccessAccountCommon(ZimbraSoapContext zsc, Account target, boolean allowSelf) throws ServiceException
/*     */   {
/* 221 */     if ((zsc.getAuthtokenAccountId() == null) || (target == null)) {
/* 222 */       return Boolean.FALSE;
/*     */     }
/*     */     
/* 225 */     if ((allowSelf) && (target.getId().equals(zsc.getAuthtokenAccountId()))) {
/* 226 */       return Boolean.TRUE;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 233 */     if ((!zsc.getAuthToken().isDelegatedAuth()) && (!zsc.isDelegatedRequest())) {
/* 234 */       logAuditAccess(null, zsc.getAuthtokenAccountId(), target.getId());
/*     */     }
/*     */     
/* 237 */     return null;
/*     */   }
/*     */   
/*     */   public boolean canAccessAccount(ZimbraSoapContext zsc, Account target) throws ServiceException {
/* 241 */     Boolean canAccess = canAccessAccountCommon(zsc, target, true);
/* 242 */     if (canAccess != null) {
/* 243 */       return canAccess.booleanValue();
/*     */     }
/* 245 */     return AccessManager.getInstance().canAccessAccount(zsc.getAuthToken(), target);
/*     */   }
/*     */   
/*     */   public boolean canModifyOptions(ZimbraSoapContext zsc, Account acct) throws ServiceException {
/* 249 */     if (zsc.isDelegatedRequest())
/*     */     {
/*     */ 
/* 252 */       return (canAccessAccount(zsc, acct)) && (getAuthenticatedAccount(zsc).getBooleanAttr("zimbraFeatureOptionsEnabled", true));
/*     */     }
/*     */     
/* 255 */     return acct.getBooleanAttr("zimbraFeatureOptionsEnabled", true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean domainAuthSufficient(Map<String, Object> context)
/*     */   {
/* 263 */     return false;
/*     */   }
/*     */   
/*     */   public boolean isAdminCommand()
/*     */   {
/* 268 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean isReadOnly()
/*     */   {
/* 274 */     return true;
/*     */   }
/*     */   
/*     */   protected boolean clientIsLocal(Map<String, Object> context)
/*     */   {
/* 279 */     HttpServletRequest req = (HttpServletRequest)context.get("servlet.request");
/* 280 */     return req == null ? true : "127.0.0.1".equals(req.getRemoteAddr());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public Session updateAuthenticatedAccount(ZimbraSoapContext zsc, AuthToken authToken, Map<String, Object> context, boolean getSession)
/*     */   {
/* 296 */     String oldAccountId = zsc.getAuthtokenAccountId();
/* 297 */     String accountId = authToken.getAccountId();
/* 298 */     if ((accountId != null) && (!accountId.equals(oldAccountId))) {
/* 299 */       zsc.clearSessionInfo();
/*     */     }
/* 301 */     zsc.setAuthToken(authToken);
/*     */     
/* 303 */     Session session = getSession ? getSession(zsc) : null;
/* 304 */     if (context != null) {
/* 305 */       context.put("zimbra.session", session);
/*     */     }
/* 307 */     return session;
/*     */   }
/*     */   
/*     */   public static Session getReferencedSession(ZimbraSoapContext zsc) {
/* 311 */     if (zsc == null) {
/* 312 */       return null;
/*     */     }
/* 314 */     ZimbraSoapContext.SessionInfo sinfo = zsc.getSessionInfo();
/* 315 */     return sinfo == null ? null : SessionCache.lookup(sinfo.sessionId, zsc.getAuthtokenAccountId());
/*     */   }
/*     */   
/*     */   public Session.Type getDefaultSessionType() {
/* 319 */     return Session.Type.SOAP;
/*     */   }
/*     */   
/*     */   protected final Session getSession(ZimbraSoapContext zsc, Map<String, Object> context) {
/* 323 */     Session session = (Session)context.get("zimbra.session");
/* 324 */     return session != null ? session : getSession(zsc);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected final Session getSession(ZimbraSoapContext zsc)
/*     */   {
/* 334 */     return getSession(zsc, getDefaultSessionType());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected Session getSession(ZimbraSoapContext zsc, Session.Type stype)
/*     */   {
/* 350 */     if ((zsc == null) || (stype == null) || (!zsc.isNotificationEnabled())) {
/* 351 */       return null;
/*     */     }
/* 353 */     String authAccountId = zsc.getAuthtokenAccountId();
/* 354 */     if (authAccountId == null) {
/* 355 */       return null;
/*     */     }
/*     */     
/*     */ 
/* 359 */     boolean isLocal = zsc.isAuthUserOnLocalhost();
/* 360 */     if ((stype == Session.Type.SOAP) && (!isLocal) && (!zsc.isSessionProxied())) {
/* 361 */       return null;
/*     */     }
/*     */     
/* 364 */     Session s = null;
/*     */     
/*     */ 
/* 367 */     ZimbraSoapContext.SessionInfo sinfo = zsc.getSessionInfo();
/* 368 */     if (sinfo != null) {
/* 369 */       s = SessionCache.lookup(sinfo.sessionId, authAccountId);
/* 370 */       if (s == null)
/*     */       {
/* 372 */         ZimbraLog.session.info("requested session no longer exists: " + sinfo.sessionId);
/* 373 */         zsc.clearSessionInfo();
/* 374 */       } else if (s.getSessionType() != stype)
/*     */       {
/* 376 */         s = null;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 381 */     if (s == null) {
/*     */       try {
/* 383 */         if (stype == Session.Type.SOAP) {
/* 384 */           s = SoapSessionFactory.getInstance().getSoapSession(zsc).register();
/* 385 */         } else if (stype == Session.Type.ADMIN) {
/* 386 */           s = new AdminSession(authAccountId).register();
/*     */         }
/*     */       } catch (ServiceException e) {
/* 389 */         ZimbraLog.session.info("exception while creating session", e);
/*     */       }
/* 391 */       if (s != null) {
/* 392 */         zsc.recordNewSession(s.getSessionId());
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 398 */     if (((s instanceof SoapSession)) && (zsc.isDelegatedRequest())) {
/* 399 */       Session delegate = ((SoapSession)s).getDelegateSession(zsc.getRequestedAccountId());
/* 400 */       if (delegate != null) {
/* 401 */         s = delegate;
/*     */       }
/*     */     }
/*     */     
/* 405 */     return s;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected void endSession(Session s)
/*     */   {
/* 414 */     SessionCache.clearSession(s);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected static Server getServer(String acctId)
/*     */     throws ServiceException
/*     */   {
/* 430 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.id, acctId);
/* 431 */     if (acct == null) {
/* 432 */       throw AccountServiceException.NO_SUCH_ACCOUNT(acctId);
/*     */     }
/*     */     
/* 435 */     String hostname = acct.getAttr("zimbraMailHost");
/* 436 */     if (hostname == null) {
/* 437 */       throw ServiceException.PROXY_ERROR(AccountServiceException.NO_SUCH_SERVER(""), "");
/*     */     }
/* 439 */     Server server = Provisioning.getInstance().get(Key.ServerBy.name, hostname);
/* 440 */     if (server == null) {
/* 441 */       throw ServiceException.PROXY_ERROR(AccountServiceException.NO_SUCH_SERVER(hostname), "");
/*     */     }
/* 443 */     return server;
/*     */   }
/*     */   
/*     */   protected static String getXPath(Element request, String[] xpath) {
/* 447 */     int depth = 0;
/* 448 */     while ((depth < xpath.length - 1) && (request != null)) {
/* 449 */       request = request.getOptionalElement(xpath[(depth++)]);
/*     */     }
/* 451 */     return request == null ? null : request.getAttribute(xpath[depth], null);
/*     */   }
/*     */   
/*     */   protected static Element getXPathElement(Element request, String[] xpath) {
/* 455 */     int depth = 0;
/* 456 */     while ((depth < xpath.length) && (request != null)) {
/* 457 */       request = request.getOptionalElement(xpath[(depth++)]);
/*     */     }
/* 459 */     return request;
/*     */   }
/*     */   
/*     */   protected static void setXPath(Element request, String[] xpath, String value) throws ServiceException {
/* 463 */     if ((xpath == null) || (xpath.length == 0)) {
/* 464 */       return;
/*     */     }
/* 466 */     int depth = 0;
/* 467 */     while ((depth < xpath.length - 1) && (request != null)) {
/* 468 */       request = request.getOptionalElement(xpath[(depth++)]);
/*     */     }
/* 470 */     if (request == null) {
/* 471 */       throw ServiceException.INVALID_REQUEST("could not find path", null);
/*     */     }
/* 473 */     request.addAttribute(xpath[depth], value);
/*     */   }
/*     */   
/*     */   protected Element proxyIfNecessary(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/* 478 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/* 479 */     String acctId = zsc.getRequestedAccountId();
/* 480 */     Provisioning.Reasons reasons = new Provisioning.Reasons();
/* 481 */     if ((acctId != null) && (zsc.getProxyTarget() == null) && (!isAdminCommand()) && (!Provisioning.onLocalServer(getRequestedAccount(zsc), reasons)))
/*     */     {
/* 483 */       if (null == zsc.getSoapRequestId())
/*     */       {
/*     */ 
/* 486 */         zsc.setSoapRequestId(Integer.toHexString(new Random().nextInt(2147483646)));
/* 487 */         ZimbraLog.addSoapIdToContext(zsc.getSoapRequestId());
/*     */       }
/* 489 */       if ((zsc.getHopCount() > 2) || (ZimbraLog.soap.isDebugEnabled())) {
/* 490 */         Account authAcct = getAuthenticatedAccount(zsc);
/* 491 */         if (authAcct == null) {
/* 492 */           ZimbraLog.soap.info("Proxying request: requestedAccountId=%s authAcct <null> reasons:%s", new Object[] { acctId, reasons.getReason() });
/*     */         }
/*     */         else {
/* 495 */           ZimbraLog.soap.info("Proxying request: requestedAccountId=%s authAcct name=%s id=%s reasons:%s", new Object[] { acctId, authAcct.getName(), authAcct.getId(), reasons.getReason() });
/*     */         }
/*     */       }
/*     */       
/* 499 */       return proxyRequest(request, context, acctId);
/*     */     }
/*     */     
/* 502 */     return null;
/*     */   }
/*     */   
/*     */   protected Element proxyRequest(Element request, Map<String, Object> context, String acctId) throws ServiceException {
/* 506 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*     */ 
/*     */ 
/* 510 */     ZimbraSoapContext zscTarget = new ZimbraSoapContext(zsc, acctId);
/*     */     
/* 512 */     return proxyRequest(request, context, getServer(acctId), zscTarget);
/*     */   }
/*     */   
/*     */   protected Element proxyRequest(Element request, Map<String, Object> context, AuthToken authToken, String acctId) throws ServiceException
/*     */   {
/* 517 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*     */ 
/*     */ 
/* 521 */     ZimbraSoapContext zscTarget = new ZimbraSoapContext(zsc, authToken, acctId, null);
/*     */     
/* 523 */     return proxyRequest(request, context, getServer(acctId), zscTarget);
/*     */   }
/*     */   
/*     */   protected Element proxyRequest(Element request, Map<String, Object> context, Server server) throws ServiceException
/*     */   {
/* 528 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*     */ 
/* 531 */     ZimbraSoapContext pxyCtxt = new ZimbraSoapContext(zsc);
/* 532 */     return proxyRequest(request, context, server, pxyCtxt);
/*     */   }
/*     */   
/*     */   protected String getProxyAuthToken(String requestedAccountId, Map<String, Object> context) throws ServiceException {
/* 536 */     return Provisioning.getInstance().getProxyAuthToken(requestedAccountId, context);
/*     */   }
/*     */   
/*     */   protected Element proxyRequest(Element request, Map<String, Object> context, Server server, ZimbraSoapContext zsc)
/*     */     throws ServiceException
/*     */   {
/* 542 */     SoapEngine engine = (SoapEngine)context.get("zimbra.engine");
/* 543 */     boolean isLocal = getLocalHostId().equalsIgnoreCase(server.getId());
/*     */     
/*     */ 
/* 546 */     if (isLocal) {
/* 547 */       zsc.resetProxyAuthToken();
/*     */     }
/*     */     
/*     */ 
/* 551 */     if (zsc.getRequestedAccountId() != null) {
/*     */       try {
/* 553 */         AuthToken at = zsc.getAuthToken();
/* 554 */         String proxyToken = getProxyAuthToken(zsc.getRequestedAccountId(), context);
/* 555 */         if ((at != null) && ((at.getProxyAuthToken() == null) || (!at.getProxyAuthToken().equals(proxyToken)))) {
/* 556 */           at.setProxyAuthToken(proxyToken);
/*     */         }
/*     */       } catch (ServiceException se) {
/* 559 */         ZimbraLog.soap.warn("failed to set proxy auth token", se);
/*     */       }
/*     */     }
/*     */     
/* 563 */     Element response = null;
/* 564 */     request.detach();
/* 565 */     if ((isLocal) && (engine != null))
/*     */     {
/* 567 */       Map<String, Object> contextTarget = new HashMap(context);
/* 568 */       contextTarget.put("zimbra.engine", engine);
/* 569 */       contextTarget.put("zimbra.context", zsc);
/* 570 */       if (ZimbraLog.soap.isDebugEnabled()) {
/* 571 */         ZimbraLog.soap.debug("Proxying request locally: targetServer=%s (id=%s) localHost=%s (id=%s)", new Object[] { server.getName(), server.getId(), LOCAL_HOST, LOCAL_HOST_ID });
/*     */       }
/*     */       
/* 574 */       response = engine.dispatchRequest(request, contextTarget, zsc);
/* 575 */       if (zsc.getResponseProtocol().isFault(response)) {
/* 576 */         zsc.getResponseProtocol().updateArgumentsForRemoteFault(response, zsc.getRequestedAccountId());
/* 577 */         throw new SoapFaultException("error in proxied request", true, response);
/*     */       }
/*     */     }
/*     */     else {
/* 581 */       preProxy(request, context);
/*     */       
/* 583 */       HttpServletRequest httpreq = (HttpServletRequest)context.get("servlet.request");
/* 584 */       ProxyTarget proxy = new ProxyTarget(server.getId(), zsc.getAuthToken(), httpreq);
/* 585 */       if (this.proxyTimeout >= 0L) {
/* 586 */         proxy.setTimeouts(this.proxyTimeout);
/*     */       }
/* 588 */       response = proxyWithNotification(request, proxy, zsc, (Session)context.get("zimbra.session"));
/*     */       
/* 590 */       postProxy(request, response, context);
/*     */     }
/* 592 */     return response;
/*     */   }
/*     */   
/*     */   public static Element proxyWithNotification(Element request, ProxyTarget proxy, ZimbraSoapContext zscProxy, ZimbraSoapContext zscInbound) throws ServiceException
/*     */   {
/* 597 */     return proxyWithNotification(request, proxy, zscProxy, getReferencedSession(zscInbound));
/*     */   }
/*     */   
/*     */   public static Element proxyWithNotification(Element request, ProxyTarget proxy, ZimbraSoapContext zscProxy, Session localSession) throws ServiceException
/*     */   {
/* 602 */     Server server = proxy.getServer();
/* 603 */     boolean isLocal = getLocalHostId().equalsIgnoreCase(server.getId());
/*     */     
/* 605 */     if (isLocal) {
/* 606 */       zscProxy.resetProxyAuthToken();
/*     */     }
/*     */     
/* 609 */     if (zscProxy.isNotificationEnabled())
/*     */     {
/* 611 */       if ((localSession instanceof SoapSession.DelegateSession)) {
/* 612 */         localSession = ((SoapSession.DelegateSession)localSession).getParentSession();
/*     */       }
/*     */       
/* 615 */       if ((!(localSession instanceof SoapSession)) || (localSession.getMailbox() == null)) {
/* 616 */         zscProxy.disableNotifications();
/* 617 */       } else if (!isLocal) {
/* 618 */         zscProxy.setProxySession(((SoapSession)localSession).getRemoteSessionId(server));
/*     */       } else {
/* 620 */         zscProxy.setProxySession(localSession.getSessionId());
/*     */       }
/*     */     }
/*     */     
/* 624 */     Pair<Element, Element> envelope = proxy.execute(request, zscProxy);
/*     */     
/* 626 */     if (((localSession instanceof SoapSession)) && (zscProxy.isNotificationEnabled()))
/* 627 */       ((SoapSession)localSession).handleRemoteNotifications(server, (Element)envelope.getFirst());
/* 628 */     return ((Element)envelope.getSecond()).detach();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String getAccountLogName(Provisioning prov, String acctId)
/*     */   {
/* 642 */     if (acctId == null) {
/* 643 */       return "";
/*     */     }
/*     */     try
/*     */     {
/* 647 */       Account acct = prov.get(Key.AccountBy.id, acctId);
/* 648 */       if (acct != null) {
/* 649 */         return acct.getName();
/*     */       }
/*     */     }
/*     */     catch (ServiceException e) {}
/* 653 */     return acctId;
/*     */   }
/*     */   
/*     */   public void logAuditAccess(String delegatingAcctId, String authedAcctId, String targetAcctId) {
/* 657 */     if (!ZimbraLog.misc.isInfoEnabled()) {
/* 658 */       return;
/*     */     }
/*     */     
/* 661 */     String reqName = this.responseQName.getQualifiedName().substring(0, this.responseQName.getQualifiedName().length() - 8);
/*     */     
/* 663 */     Provisioning prov = Provisioning.getInstance();
/* 664 */     String delegatingAcctName = getAccountLogName(prov, delegatingAcctId);
/* 665 */     String authedAcctName = getAccountLogName(prov, authedAcctId);
/* 666 */     String targetAcctName = getAccountLogName(prov, targetAcctId);
/*     */     
/* 668 */     ZimbraLog.misc.info("delegated access: doc=" + reqName + (delegatingAcctId == null ? "" : new StringBuilder().append(", delegating account=").append(delegatingAcctName).toString()) + ", authenticated account=" + authedAcctName + ", target account=" + targetAcctName);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   protected static Pair<String, Version> zimbraConnectorClientVersion(ZimbraSoapContext zsc)
/*     */   {
/* 675 */     String UA_ZCO = "ZimbraConnectorForOutlook";
/* 676 */     String UA_ZCB = "ZimbraConnectorForBES";
/* 677 */     String UA_MIGRATION = "ZimbraMigration";
/*     */     
/* 679 */     String ua = zsc.getUserAgent();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 685 */     if (ua != null) {
/* 686 */       String[] parts = ua.split("/");
/* 687 */       if (parts.length == 2) {
/* 688 */         String app = parts[0];
/* 689 */         String version = parts[1];
/*     */         
/* 691 */         if (("ZimbraConnectorForOutlook".equalsIgnoreCase(app)) || ("ZimbraConnectorForBES".equalsIgnoreCase(app)) || ("ZimbraMigration".equalsIgnoreCase(app))) {
/*     */           try {
/* 693 */             return new Pair(app, new Version(version, false));
/*     */           } catch (ServiceException e) {
/* 695 */             ZimbraLog.soap.debug("unable to parse zimbra connector client version", e);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 700 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */   @VisibleForTesting
/*     */   public static void resetLocalHost()
/*     */   {
/* 707 */     LOCAL_HOST = "";
/* 708 */     LOCAL_HOST_ID = "";
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/soap/DocumentHandler.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */